package 晚间作业0314;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class Doc10 {
    /**
     * 3. 设计一个动物类，属性: 姓名，性别
     * 设计一个猫类，设计一个狗类， 猫和狗都继承自动物类
     * 需求：在测试类中设计一个方法，这个方法需要一个参数，参数的类型可以是猫类，也可以是狗类 -->多态(动
     * 物类 )
     * 在方法体内，将传入的猫或者狗的属性输出即可 输出到底是猫还是狗
     */
    public static class P03 {
        public static class Animal {
            protected String name;
            protected String gender;
        }
        public static class Cat extends Animal {
            @Override
            public String toString() {
                return "我是猫，" + name + "," + gender;
            }
        }
        public static class Dog extends Animal {
            @Override
            public String toString() {
                return "我是狗，" + name + "," + gender;
            }
        }

        public static void show(Animal animal) {
            if (animal instanceof Cat) {
                System.out.println("是猫");
                System.out.println(animal);
            } else if (animal instanceof Dog){
                System.out.println("是狗");
                System.out.println(animal);
            } else {
                System.out.println("这是啥(⊙o⊙)…");
            }
        }

        public static void main(String[] args) {
            Animal cat = new Cat();
            Animal dog = new Dog();
            show(cat);
            show(dog);
        }
    }


    /**
     * 4. 设计一个数据传输接口：Type-C标准
     * 设计一个防水的接口：防水
     * 设计一个手机类，实现这个数据传输接口，采用Type-C；同时实现了防水的接口
     */
    public static class P04 {
        public interface SerialPort { void transDatum(String data);}
        public interface WaterProof { void fun(); }

        public static class Phone implements SerialPort, WaterProof {
            @Override
            public void transDatum(String data) {
                System.out.println("使用Type_C传输了数据：" + data);
            }

            @Override
            public void fun() {
                System.out.println("我是防水的！！");
            }
        }

        public static void main(String[] args) {
            Phone phone = new Phone();
            phone.transDatum("data");
            phone.fun();
        }
    }


    /**
     * 5. （难）利用接口做参数，写个计算器，能完成加减乘除运算
     * 5.1定义一个接口 Compute，含有一个计算方法 int compute(int a, int b)
     * 5.2设计一个类 UseCompute，含有方法 public void useCom(Compute com, int one, int two) ，此方法
     * 能够用传递过来的对象调用 compute 方法完成运算，并输出计算的结果。
     * 5.3设计一个主类 Test，调用 UseCompute 中的方法 useCom 来完成加减乘除运算。
     */
    public static class P05 {
        public interface Compute {
            double compute(int a, int b);
        }

        public static class UseCompute {
            public void useCom(Compute com, int one, int two) {
                System.out.println("计算结果：" + com.compute(one, two));
            }
        }

        public static void main(String[] args) {
            int one = 6, two = 2;
            new UseCompute().useCom((a, b) -> a + b, one, two);// +
            new UseCompute().useCom((a, b) -> a - b, one, two);// -
            new UseCompute().useCom((a, b) -> a * b, one, two);// *
            // /
            new UseCompute().useCom((a, b) -> {
                if (b == 0) { return -1; }
                return (double) a / (double) b;
            }, one, two);
        }
    }


    /**
     * 班级学生排队,男生按照身高从低到高排一队,女生,按照姓名从小到大排一队(使用接口和匿名内部类)(选做)
     */
    public static class PExtra {
        public interface SComparable {
            int compareTo(Student student);
        }

        public static class Student implements SComparable{
            protected String name;
            protected int height;
            protected String gender;

            public Student(String name, int height, String gender) {
                this.name = name;
                this.height = height;
                this.gender = gender;
            }

            @Override
            public String toString() {
                return "{" +
                        "name='" + name + '\'' +
                        ", height=" + height +
                        ", gender='" + gender + '\'' +
                        '}';
            }

            @Override
            public int compareTo(Student student) {
                return 0;
            }

            public static void sort(List<? extends Student> students) {

            }
        }

        public static class Male extends Student {
            public Male(String name, int height) {super(name, height, "男");}

            @Override
            public int compareTo(Student student) {
                return this.height - student.height;
            }
        }

        public static class Female extends Student {
            public Female(String name, int height) {super(name, height, "女");}

            @Override
            public int compareTo(Student student) {
                return 0;
            }
        }

        public static void main(String[] args) {
            //男生
            List<Male> males = new ArrayList<>();
            males.add(new Male("小明", 170));
            males.add(new Male("小张", 171));
            males.add(new Male("小李", 165));
            males.add(new Male("小王", 180));
            System.out.println(males.stream().sorted(Comparator.comparingInt(o -> o.height)).collect(Collectors.toList()));

            List<Female> females = new ArrayList<>();
            females.add(new Female("王红", 162));
            females.add(new Female("张艳艳", 162));
            females.add(new Female("李冰冰", 162));
            System.out.println(females.stream().sorted(Comparator.comparing(o -> o.name)).collect(Collectors.toList()));
        }
    }
}
